package com.boc.ewr.outsrc.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.boc.ewr.outsrc.entity.OutsrcStateAnalysis;
import com.boc.ewr.outsrc.service.IOutsrcStateAnalysisService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;

/**
 * @Description: 外包公司情况分析表
 * @Author: jeecg-boot
 * @Date: 2023-06-18
 * @Version: V1.0
 */
@Api(tags = "外包公司情况分析表")
@RestController
@RequestMapping("/outsrc/outsrcStateAnalysis")
@Slf4j
public class OutsrcStateAnalysisController extends JeecgController<OutsrcStateAnalysis, IOutsrcStateAnalysisService> {
    @Autowired
    private IOutsrcStateAnalysisService outsrcStateAnalysisService;

    /**
     * 分页列表查询
     *
     * @param outsrcStateAnalysis
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "外包公司情况分析表-分页列表查询")
    @ApiOperation(value = "外包公司情况分析表-分页列表查询", notes = "外包公司情况分析表-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<OutsrcStateAnalysis>> queryPageList(OutsrcStateAnalysis outsrcStateAnalysis,
                                                            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                            HttpServletRequest req) {
        QueryWrapper<OutsrcStateAnalysis> queryWrapper = QueryGenerator.initQueryWrapper(outsrcStateAnalysis, req.getParameterMap());
        Page<OutsrcStateAnalysis> page = new Page<OutsrcStateAnalysis>(pageNo, pageSize);
        IPage<OutsrcStateAnalysis> pageList = outsrcStateAnalysisService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 生成数据
     *
     * @param start
     * @param end
     * @return
     */
    //@AutoLog(value = "外包公司情况分析表-生成数据")
    @ApiOperation(value = "外包公司情况分析表-生成数据", notes = "外包公司情况分析表-生成数据")
    @GetMapping(value = "/genData")
    public Result<String> genData(@RequestParam(name = "start", defaultValue = "") String start, @RequestParam(name = "end", defaultValue = "") String end) {
        Date startDate = new Date();
        Date endDate = new Date();

        if ("".equals(start) && "".equals(end)) {
            Calendar calendar = Calendar.getInstance();
            endDate = calendar.getTime();
            calendar.add(Calendar.MONTH, -6);
            startDate = calendar.getTime();
        } else {
            String pattern = "yyyy-MM-dd";
            SimpleDateFormat sdf = new SimpleDateFormat(pattern);
            try {
                startDate = sdf.parse(start);
                endDate = sdf.parse(end);
            } catch (ParseException e) {
                e.printStackTrace();
                return Result.error("时间格式转化出错！");
            }

        }

        outsrcStateAnalysisService.generateInfo(startDate, endDate);
        return Result.OK();
    }

    /**
     * 添加
     *
     * @param outsrcStateAnalysis
     * @return
     */
    @AutoLog(value = "外包公司情况分析表-添加")
    @ApiOperation(value = "外包公司情况分析表-添加", notes = "外包公司情况分析表-添加")
    //@RequiresPermissions("org.jeecg.modules:outsrc_state_analysis:add")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody OutsrcStateAnalysis outsrcStateAnalysis) {
        outsrcStateAnalysisService.save(outsrcStateAnalysis);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param outsrcStateAnalysis
     * @return
     */
    @AutoLog(value = "外包公司情况分析表-编辑")
    @ApiOperation(value = "外包公司情况分析表-编辑", notes = "外包公司情况分析表-编辑")
    //@RequiresPermissions("org.jeecg.modules:outsrc_state_analysis:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody OutsrcStateAnalysis outsrcStateAnalysis) {
        outsrcStateAnalysisService.updateById(outsrcStateAnalysis);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "外包公司情况分析表-通过id删除")
    @ApiOperation(value = "外包公司情况分析表-通过id删除", notes = "外包公司情况分析表-通过id删除")
    //@RequiresPermissions("org.jeecg.modules:outsrc_state_analysis:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        outsrcStateAnalysisService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "外包公司情况分析表-批量删除")
    @ApiOperation(value = "外包公司情况分析表-批量删除", notes = "外包公司情况分析表-批量删除")
    //@RequiresPermissions("org.jeecg.modules:outsrc_state_analysis:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.outsrcStateAnalysisService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "外包公司情况分析表-通过id查询")
    @ApiOperation(value = "外包公司情况分析表-通过id查询", notes = "外包公司情况分析表-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<OutsrcStateAnalysis> queryById(@RequestParam(name = "id", required = true) String id) {
        OutsrcStateAnalysis outsrcStateAnalysis = outsrcStateAnalysisService.getById(id);
        if (outsrcStateAnalysis == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(outsrcStateAnalysis);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param outsrcStateAnalysis
     */
    //@RequiresPermissions("org.jeecg.modules:outsrc_state_analysis:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, OutsrcStateAnalysis outsrcStateAnalysis) {
        return super.exportXls(request, outsrcStateAnalysis, OutsrcStateAnalysis.class, "外包公司情况分析表");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    //@RequiresPermissions("outsrc_state_analysis:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, OutsrcStateAnalysis.class);
    }

}
